package com.loongtech.bi.action.report;

import com.loongtech.bi.manager.report.OverviewReportManager;
import com.loongtech.bi.manager.report.OverviewReportManager.*;
import com.loongtech.bi.manager.system.SysLogManager;
import com.loongtech.bi.support.Session;
import com.loongtech.bi.support.chart.ChartTool;
import com.loongtech.bi.support.chart.ChartType;
import com.loongtech.bi.support.chart.ChartUIData;
import com.loongtech.core.util.Time;
import org.primefaces.event.TabChangeEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;

/**
 * 运营报表-概况Action
 * @author Marin
 * @date 2015年09月17日
 */
@Controller
@Scope("view")
public class OverviewReportAction {
    private String queryPlatform = "0";  // 前端传过来的查询条件：平台
    private String queryChannel = "0";  // 前端传过来的查询条件：渠道
    private String queryServer = "0";  // 前端传过来的查询条件：区服

	public final static String tab1 = "pay";  // 付费额
	public final static String tab4 = "payUser";  // 付费人数
	public final static String tab2 = "payRadio";  // 付费率&新增当日付费率
	public final static String tab3 = "payArpu";  // 付费ARPU&活跃ARPU
	
	private String dateRadio = "7";  // 切换的天数：7=近7天。15=近15天。30=近30天，0=自选时间
	// private int slideDay = 1;  // 自选时间时，选择的天数
	private Date dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - 7 * Time.DAY));  // 自选时间：开始时间
	private Date dateEnd = new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY));  // 自选时间：结束时间
	
	private ChartUIData chartUIdata1;  // 在线趋势图
	private ChartUIData chartUIdata2;  // 留存情况图
	private ChartUIData chartUIdata3;  // 活跃情况图
	private ChartUIData chartUIdata5;  // 付费情况图
	private ChartUIData chartUIdata4;  // 游戏时长图
	private List<OverviewData> datas;  // 前端要显示的实时汇总数据
    private List<YesterDayOverviewData> yesterdayDatas;  // 前端要显示的昨日数据
	private List<OnlineChartUIData> dataList1; // 在线趋势临时数据
	private List<AliveChartUIData> dataList2;  // 留存情况临时数据
	private List<ActiveChartUIData> dataList3;  // 活跃情况临时数据
	private List<PayChartUIData> dataList5;  // 收入情况临时数据
	private List<PlayTimeChartUIData> dataList4;  // 游戏时长临时数据
	
	private String tabId = "pay";  // 当前选中的tab
	
	@Autowired
	OverviewReportManager overviewReportManager;
	
	@Autowired
	private SysLogManager sysLogManager;
	@Autowired
	private Session session;
	
	@PostConstruct
	public void init() {
		search();
	}

	public void search() {
		sysLogManager.addLog(session.getUserName(), "查询运营报表-概况");
		
		chartUIdata1 = null;
		chartUIdata2 = null;
		chartUIdata3 = null;
		chartUIdata4 = null;
		chartUIdata5 = null;
		
		Date startDay = new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateStart : new Date(Time.getBeginningOfDay(Time.currentTime() - (Integer.parseInt(dateRadio) + 1) * Time.DAY))).getTime()));  // 开始时间
		Date endDay = new Date(Time.getBeginningOfDay(("0".equals(dateRadio) ? dateEnd : new Date(Time.getBeginningOfDay(Time.currentTime() - Time.DAY))).getTime()));  // 结束时间

        yesterdayDatas = overviewReportManager.getYesterdayOverviewData(queryPlatform, queryChannel, queryServer);  // 昨日汇总数据
        datas = overviewReportManager.getOverviewData(startDay, endDay);  // 汇总数据

		dataList1 = overviewReportManager.getOnlineChartUIData(startDay, endDay);
		if (dataList1 != null && dataList1.size() > 0) {
			chartUIdata1 = new ChartUIData();
			chartUIdata1.setJsonType(ChartType.SPLINE.type);
			chartUIdata1.setJsonStr(new ChartTool(dataList1, OnlineChartUIData.class, "").loadData(chartUIdata1.getJsonType()));
		}
		dataList2 = overviewReportManager.getAliveChartUIData(startDay, endDay);
		if (dataList2 != null && dataList2.size() > 0) {
			chartUIdata2 = new ChartUIData();
			chartUIdata2.setJsonType(ChartType.SPLINE.type);
			chartUIdata2.setJsonStr(new ChartTool(dataList2, AliveChartUIData.class, "").loadData(chartUIdata2.getJsonType()));
		}
		dataList3 = overviewReportManager.getActiveChartUIData(startDay, endDay);
		if (dataList3 != null && dataList3.size() > 0) {
			chartUIdata3 = new ChartUIData();
			chartUIdata3.setJsonType(ChartType.COLUMN.type);
			chartUIdata3.setJsonStr(new ChartTool(dataList3, ActiveChartUIData.class, "").loadData(ChartType.COLUMN.type));
		}
		dataList5 = overviewReportManager.getPayChartUIData(startDay, endDay);
		if (dataList5 != null && dataList5.size() > 0) {
			chartUIdata5 = new ChartUIData();
			chartUIdata5.setJsonType(ChartType.SPLINE.type);
			chartUIdata5.setJsonStr(new ChartTool(dataList5, PayChartUIData.class, tabId).loadData(chartUIdata5.getJsonType()));
		}
		dataList4 = overviewReportManager.getPlayTimeChartUIData(startDay, endDay);
		if (dataList4 != null && dataList4.size() > 0) {
			chartUIdata4 = new ChartUIData();
			chartUIdata4.setJsonType(ChartType.SPLINE.type);
			chartUIdata4.setJsonStr(new ChartTool(dataList4, PlayTimeChartUIData.class, "").loadData(chartUIdata4.getJsonType()));
		}
	}
	
	/**
	 * 切换天数
	 */
	public void radioChange() {
		int stayDay = "0".equals(dateRadio) ? 2 : Integer.parseInt(dateRadio);
		dateStart = new Date(Time.getBeginningOfDay(Time.currentTime() - stayDay * Time.DAY));
		
		search();
	}
	
	/**
	 * 划动天数
	 */
	public void spinnerChange() {
		search();
	}
	
	// 切换标签页
	public void onTabChange(TabChangeEvent event) {
		tabId = event.getTab().getId();
		if (chartUIdata5 != null) {
			//chartUIdata5.setJsonType(ChartType.SPLINE.type);
			chartUIdata5.setJsonStr(new ChartTool(dataList5, PayChartUIData.class, tabId).loadData(chartUIdata5.getJsonType()));
		}
	}

	public String getDateRadio() {
		return dateRadio;
	}

	public void setDateRadio(String dateRadio) {
		this.dateRadio = dateRadio;
	}

	/*public int getSlideDay() {
		return slideDay;
	}

	public void setSlideDay(int slideDay) {
		this.slideDay = slideDay;
	}*/

	public ChartUIData getChartUIdata1() {
		return chartUIdata1;
	}

	public void setChartUIdata1(ChartUIData chartUIdata1) {
		this.chartUIdata1 = chartUIdata1;
	}

	public ChartUIData getChartUIdata2() {
		return chartUIdata2;
	}

	public void setChartUIdata2(ChartUIData chartUIdata2) {
		this.chartUIdata2 = chartUIdata2;
	}

	public ChartUIData getChartUIdata3() {
		return chartUIdata3;
	}

	public void setChartUIdata3(ChartUIData chartUIdata3) {
		this.chartUIdata3 = chartUIdata3;
	}

	public ChartUIData getChartUIdata4() {
		return chartUIdata4;
	}

	public void setChartUIdata4(ChartUIData chartUIdata4) {
		this.chartUIdata4 = chartUIdata4;
	}

	public Date getDateStart() {
		return dateStart;
	}

	public void setDateStart(Date dateStart) {
		this.dateStart = dateStart;
	}

	public Date getDateEnd() {
		return dateEnd;
	}

	public void setDateEnd(Date dateEnd) {
		this.dateEnd = dateEnd;
	}

	public ChartUIData getChartUIdata5() {
		return chartUIdata5;
	}

	public void setChartUIdata5(ChartUIData chartUIdata5) {
		this.chartUIdata5 = chartUIdata5;
	}

	public List<OnlineChartUIData> getDataList1() {
		return dataList1;
	}

	public void setDataList1(List<OnlineChartUIData> dataList1) {
		this.dataList1 = dataList1;
	}

	public List<AliveChartUIData> getDataList2() {
		return dataList2;
	}

	public void setDataList2(List<AliveChartUIData> dataList2) {
		this.dataList2 = dataList2;
	}

	public List<ActiveChartUIData> getDataList3() {
		return dataList3;
	}

	public void setDataList3(List<ActiveChartUIData> dataList3) {
		this.dataList3 = dataList3;
	}

	public List<PayChartUIData> getDataList5() {
		return dataList5;
	}

	public void setDataList5(List<PayChartUIData> dataList5) {
		this.dataList5 = dataList5;
	}

	public List<PlayTimeChartUIData> getDataList4() {
		return dataList4;
	}

	public void setDataList4(List<PlayTimeChartUIData> dataList4) {
		this.dataList4 = dataList4;
	}

	public String getTabId() {
		return tabId;
	}

	public void setTabId(String tabId) {
		this.tabId = tabId;
	}

	public List<OverviewData> getDatas() {
		return datas;
	}

	public void setDatas(List<OverviewData> datas) {
		this.datas = datas;
	}

    public List<YesterDayOverviewData> getYesterdayDatas() {
        return yesterdayDatas;
    }

    public void setYesterdayDatas(List<YesterDayOverviewData> yesterdayDatas) {
        this.yesterdayDatas = yesterdayDatas;
    }

    public String getQueryPlatform() {
        return queryPlatform;
    }

    public void setQueryPlatform(String queryPlatform) {
        this.queryPlatform = queryPlatform;
    }

    public String getQueryChannel() {
        return queryChannel;
    }

    public void setQueryChannel(String queryChannel) {
        this.queryChannel = queryChannel;
    }

    public String getQueryServer() {
        return queryServer;
    }

    public void setQueryServer(String queryServer) {
        this.queryServer = queryServer;
    }
}
